<?php

namespace Controller\Account;

class ResetPassword extends \Controller\Base {

    public function actionIndex() {
        $this->setMenuPath('Account', 'AccountList');
        if ($this->isPost()) {
            $this->reset();
        }
        $this->display();
    }

    private function reset() {
        $oldPassword = $this->postParam('oldPassword');
        $newPassword1 = $this->postParam('newPassword1');
        $newPassword2 = $this->postParam('newPassword2');
        if (!$oldPassword) {
            $this->addWarningMsg('老密码必须输入');
            return;
        }
        if ($this->encryptionPassword($oldPassword) != $this->accountInfo['Password']) {
            $this->addWarningMsg('密码错误');
            return;
        }
        if (!$newPassword1) {
            $this->addWarningMsg('请输入新密码');
            return;
        }
        $len = strlen($newPassword1);
        if ($len < 6 && $len > 16) {
            $this->addWarningMsg('密码请在8至16位之前');
            return;
        }
        if ($newPassword2 != $newPassword1) {
            $this->addWarningMsg('两次密码输入不一样');
        }
        $newPassword = $this->encryptionPassword($newPassword1);
        \Business\Account::instance()->updatePassword($this->accountId, $newPassword);
        $this->addSuccessMsg('修改成功，下次请用新密码登录');
    }

}